﻿using UnityEngine;
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

[XmlRoot("WindowDB")]
public class WindowDB {

	[XmlElement("nodes")]
	public List<Node> nodes = new List<Node>();
	
	public void Save()
	{
		
	}
	
	public void SaveNodes(Transform trans)
	{
		nodes.Clear();
		SaveNode(nodes, trans);
		Save();
	}
	
	void SaveNode(List<Node> nodes, Transform trans)
	{
		if(trans.hideFlags == HideFlags.NotEditable)
			return;
		
		switch(Node.GetNodeType(trans))
		{
			
		//window
		case NodeType.Window:
			WindowNode windowNode = new WindowNode();
			windowNode.SaveNodeInfo(trans);
			nodes.Add(windowNode);
			break;
		case NodeType.UIPanel:
			PanelNode panelNode = new PanelNode();
			panelNode.SaveNodeInfo(trans);
			nodes.Add(panelNode);
			break;
			
		//control
		case NodeType.UIMix:
			MixNode mixNode = new MixNode();
			mixNode.SaveNodeInfo(trans);
			nodes.Add(mixNode);
			break;
		case NodeType.UIButton:
			ButtonNode buttonNode = new ButtonNode();
			buttonNode.SaveNodeInfo(trans);
			nodes.Add(buttonNode);
			break;
		case NodeType.UIImageButton:
			ImageButtonNode imageButtonNode = new ImageButtonNode();
			imageButtonNode.SaveNodeInfo(trans);
			nodes.Add(imageButtonNode);
			break;
		case NodeType.UICheckbox:
			CheckboxNode checkboxNode = new CheckboxNode();
			checkboxNode.SaveNodeInfo(trans);
			nodes.Add(checkboxNode);
			break;
		case NodeType.UISlider:
			SliderNode sliderNode = new SliderNode();
			sliderNode.SaveNodeInfo(trans);
			nodes.Add(sliderNode);
			break;
		case NodeType.UIInput:
			InputNode inputNode = new InputNode();
			inputNode.SaveNodeInfo(trans);
			nodes.Add(inputNode);
			break;
		case NodeType.UIPopList:
			PopListNode popListNode = new PopListNode();
			popListNode.SaveNodeInfo(trans);
			nodes.Add(popListNode);
			break;
		case NodeType.UIScrollBar:
			ScrollBarNode scrollBarNode = new ScrollBarNode();
			scrollBarNode.SaveNodeInfo(trans);
			nodes.Add(scrollBarNode);
			break;
			
		//Label
		case NodeType.UILabel:
			LabelNode labelNode = new LabelNode();
			labelNode.SaveNodeInfo(trans);
			nodes.Add(labelNode);
			break;
			
		//Sprite
		case NodeType.UIFilledSprite:
			FilledSpriteNode filledSpriteNode = new FilledSpriteNode();
			filledSpriteNode.SaveNodeInfo(trans);
			nodes.Add(filledSpriteNode);
			break;
		case NodeType.UISlicedSprite:
			SlicedSpriteNode slicedSpriteNode = new SlicedSpriteNode();
			slicedSpriteNode.SaveNodeInfo(trans);
			nodes.Add(slicedSpriteNode);
			break;
		case NodeType.UISprite:
			SpriteNode spriteNode = new SpriteNode();
			spriteNode.SaveNodeInfo(trans);
			nodes.Add(spriteNode);
			break;
			
		//SimpleTexture
		case NodeType.UITexture:
			SimpleTextureNode simpleTextureNode = new SimpleTextureNode();
			simpleTextureNode.SaveNodeInfo(trans);
			nodes.Add(simpleTextureNode);
			break;
		default:
			Node node = new Node();
			node.SaveNodeInfo(trans);
			nodes.Add(node);
			break;
		}
		
		for(int i = 0; i < trans.childCount; i++)
		{
			SaveNode(nodes[nodes.Count - 1].children, trans.GetChild(i));
		}
	}
	
	public void UpdateNodes(Transform trans)
	{
		if(nodes.Count > 0)
			UpdateNode(nodes, trans);
	}
	
	void UpdateNode(List<Node> nodes, Transform trans)
	{
		if(trans.hideFlags == HideFlags.NotEditable)
			return;
		
		foreach(Node node in nodes)
		{
			if(node.updated)
				continue;
			
			if(node.name == trans.name)
			{
				node.UpdateNodeInfo(trans);
				
				for(int i = 0; i < trans.childCount; i++)
					UpdateNode(node.children, trans.GetChild(i));
				
				break;
			}
		}
	}
	
	
	[System.Xml.Serialization.XmlInclude(typeof(WindowNode))]
	[System.Xml.Serialization.XmlInclude(typeof(ControlNode))]
	[System.Xml.Serialization.XmlInclude(typeof(MixNode))]
	[System.Xml.Serialization.XmlInclude(typeof(SimpleTextureNode))]
	[System.Xml.Serialization.XmlInclude(typeof(ButtonNode))]
	[System.Xml.Serialization.XmlInclude(typeof(ImageButtonNode))]
	[System.Xml.Serialization.XmlInclude(typeof(CheckboxNode))]
	[System.Xml.Serialization.XmlInclude(typeof(SliderNode))]
	[System.Xml.Serialization.XmlInclude(typeof(InputNode))]
	[System.Xml.Serialization.XmlInclude(typeof(PopListNode))]
	[System.Xml.Serialization.XmlInclude(typeof(ScrollBarNode))]
	[XmlRoot("Node")]
	public class Node
	{
		[XmlAttribute("name")]
		public string name = "";
		[XmlAttribute("active")]
		public bool active = true;
		
		public Vector3 localPosition = Vector3.zero;
		public Vector3 localRotation = Vector3.zero;
		public Vector3 localScale = Vector3.one;
		
		[XmlAttribute("luaFile")]
		public string luaFile = "";
		[XmlAttribute("luaEvent")]
		public LuaUIEventMethod luaEvent = LuaUIEventMethod.None;
		
		public List<Node> children = new List<Node>();
		public bool updated = false;
		
		public Node()
		{
			name = "";
			active = true;
			
			localPosition = Vector3.zero;
			localRotation = Vector3.zero;
			localScale = Vector3.one;
			
			luaFile = "";
			luaEvent = LuaUIEventMethod.None;
			
			children = new List<Node>();
		}
		
		public static NodeType GetNodeType(Transform trans)
		{
			NodeType type = NodeType.Normal;
			
			//window
			if(trans.GetComponent<Window>() != null)
				type = NodeType.Window;
			else if(trans.GetComponent<UIPanel>() != null)
				type = NodeType.UIPanel;
			
			//control
//			else if(trans.GetComponent<UIMix>() != null)
//				type = NodeType.UIMix;
			else if(trans.GetComponent<UICheckbox>() != null)
				type = NodeType.UICheckbox;
			else if(trans.GetComponent<UIImageButton>() != null)
				type = NodeType.UIImageButton;
			else if(trans.GetComponent<UISlider>() != null)
				type = NodeType.UISlider;
			else if(trans.GetComponent<UIInput>() != null)
				type = NodeType.UIInput;
			else if(trans.GetComponent<UIPopupList>() != null)
				type = NodeType.UIPopList;
			else if(trans.GetComponent<UIScrollBar>() != null)
				type = NodeType.UIScrollBar;
			
			//Label
			else if(trans.GetComponent<UILabel>() != null)
				type = NodeType.UILabel;
			
			//Sprite
			else if(trans.GetComponent<UIFilledSprite>() != null)
				type = NodeType.UIFilledSprite;
			else if(trans.GetComponent<UITiledSprite>() != null)
				type = NodeType.UITiledSprite;
			else if(trans.GetComponent<UISlicedSprite>() != null)
				type = NodeType.UISlicedSprite;
			else if(trans.GetComponent<UISprite>() != null)
				type = NodeType.UISprite;
			
			//SimpleTexture
			else if(trans.GetComponent<UITexture>() != null)
				type = NodeType.UITexture;
			
			//common
			else if(trans.GetComponent<UIButton>() != null)
				type = NodeType.UIButton;
			
			return type;
		}
		
		public virtual void SaveNodeInfo(Transform trans)
		{
			name = trans.name;
			active = trans.gameObject.activeInHierarchy;
			
			localPosition = trans.localPosition;
			localRotation = trans.localRotation.eulerAngles;
			localScale = trans.localScale;
			
			LuaUI lua = trans.GetComponent<LuaUI>();
			if(lua != null)
			{
				luaFile = lua.luaFile;
				if(lua.methods.Count > 0)
					luaEvent = lua.methods[0];
			}
		}
		
		public virtual void UpdateNodeInfo(Transform trans)
		{
			updated = true;
			trans.gameObject.SetActive(active);
			
			trans.localPosition = localPosition;
			trans.localRotation = Quaternion.Euler(localRotation);
			trans.localScale = localScale;
			
			LuaUI lua = trans.GetComponent<LuaUI>();
			if(luaEvent != LuaUIEventMethod.None)
			{
				if(lua == null)
					lua = trans.gameObject.AddComponent<LuaUI>();
				
				lua.methods.Clear();
				lua.methods.Add(luaEvent);
				lua.luaFile = luaFile;
			}
			else
			{
				if(lua != null)
					MonoBehaviour.Destroy(lua);
			}
		}
	}
	
	
	[System.Xml.Serialization.XmlInclude(typeof(WindowNode))]
	[XmlRoot("PanelNode")]
	public class PanelNode : Node
	{
		public UIDrawCall.Clipping clipping = UIDrawCall.Clipping.None;
		public Vector4 clipRange = Vector4.zero;
		public Vector2 clipSoftness = new Vector2(40f, 40f);
		
		public PanelNode() : base()
		{
			clipping = UIDrawCall.Clipping.None;
			clipRange = Vector4.zero;
			clipSoftness = new Vector2(40f, 40f);
		}
		
		public override void SaveNodeInfo(Transform trans)
		{
			base.SaveNodeInfo(trans);
			
			UIPanel panel = trans.GetComponent<UIPanel>();
			if(panel == null)
				Debug.LogError(panel.gameObject);
			
			clipping = panel.clipping;
			clipRange = panel.clipRange;
			clipSoftness = panel.clipSoftness;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIPanel panel = trans.GetComponent<UIPanel>();
			panel.clipping = clipping;
			panel.clipRange = clipRange;
			panel.clipSoftness = clipSoftness;
		}
	}
	
	[XmlRoot("WindowNode")]
	public class WindowNode : PanelNode
	{
		public WindowNode() : base()
		{
			
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
		}
	}
	
	[System.Xml.Serialization.XmlInclude(typeof(LabelNode))]
	[System.Xml.Serialization.XmlInclude(typeof(SpriteNode))]
	[XmlRoot("ControlNode")]
	public class ControlNode : Node
	{
		public int depth = 0;
		public Color color = Color.white;
		public UIWidget.Pivot pivot = UIWidget.Pivot.Center;
		
		public ControlNode() : base()
		{
			depth = 0;
			color = Color.white;
			pivot = UIWidget.Pivot.Center;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UIWidget widget = trans.GetComponent<UIWidget>();
			depth = widget.depth;
			color = widget.color;
			pivot = widget.pivot;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIWidget widget = trans.GetComponent<UIWidget>();
			widget.depth = depth;
			widget.color = color;
			widget.pivot = pivot;
		}
	}
	
	[XmlRoot("LabelNode")]
	public class LabelNode : ControlNode
	{
		public string text = "";
		public int lineWidth = 0;
		public bool multiLine = true;
//		public UILabel.TypingCustom typingCustom = UILabel.TypingCustom.Chinese;
		public int spacX = 0;
		public int spacY = 0;
		public bool password = false;
		public bool supportEncoding = true;
		public UIFont.SymbolStyle symbolStyle = UIFont.SymbolStyle.None;
		public UILabel.Effect effectStyle = UILabel.Effect.None;
		public Color effectColor = Color.black;
		
		public LabelNode() : base()
		{
			text = "";
			lineWidth = 0;
			multiLine = true;
//			UILabel.TypingCustom typingCustom = UILabel.TypingCustom.Chinese;
//			spacX = 0;
//			spacY = 0;
			password = false;
			supportEncoding = true;
			UIFont.SymbolStyle symbolStyle = UIFont.SymbolStyle.None;
			UILabel.Effect effectStyle = UILabel.Effect.None;
			effectColor = Color.black;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UILabel label = trans.GetComponent<UILabel>();
			text = label.text;
			lineWidth = label.lineWidth;
			multiLine = label.multiLine;
//			UILabel.TypingCustom typingCustom = label.typingCustom;
//			spacX = label.spacX;
//			spacY = label.spacY;
			password = label.password;
			supportEncoding = label.supportEncoding;
			UIFont.SymbolStyle symbolStyle = label.symbolStyle;
			UILabel.Effect effectStyle = label.effectStyle;
			effectColor = label.effectColor;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UILabel label = trans.GetComponent<UILabel>();
			label.text = text;
			label.lineWidth = lineWidth;
			label.multiLine = multiLine;
//			label.typingCustom = typingCustom;
//			label.spacX = spacX;
//			label.spacY = spacY;
			label.password = password;
			label.supportEncoding = supportEncoding;
			label.symbolStyle = symbolStyle;
			label.effectStyle = effectStyle;
			label.effectColor = effectColor;
		}
	}
	
	[System.Xml.Serialization.XmlInclude(typeof(SlicedSpriteNode))]
	[System.Xml.Serialization.XmlInclude(typeof(TiledSpriteNode))]
	[System.Xml.Serialization.XmlInclude(typeof(FilledSpriteNode))]
	[XmlRoot("SpriteNode")]
	public class SpriteNode : ControlNode
	{
		public string spriteNmae = "";
		
		public SpriteNode() : base()
		{
			spriteNmae = "";
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UISprite sprite = trans.GetComponent<UISprite>();
			spriteNmae = sprite.spriteName;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UISprite sprite = trans.GetComponent<UISprite>();
			sprite.spriteName = spriteNmae;
		}
	}
	
	[XmlRoot("SlicedSpriteNode")]
	public class SlicedSpriteNode : SpriteNode
	{
		public bool fillCenter = true;
		
		public SlicedSpriteNode() : base()
		{
			fillCenter = true;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UISlicedSprite sprite = trans.GetComponent<UISlicedSprite>();
			fillCenter = sprite.fillCenter;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UISlicedSprite sprite = trans.GetComponent<UISlicedSprite>();
			sprite.fillCenter = fillCenter;
		}
	}
	
	[XmlRoot("TiledSpriteNode")]
	public class TiledSpriteNode : SpriteNode
	{
		public bool fillCenter = true;
		
		public TiledSpriteNode() : base()
		{
			fillCenter = true;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UITiledSprite sprite = trans.GetComponent<UITiledSprite>();
			fillCenter = sprite.fillCenter;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UITiledSprite sprite = trans.GetComponent<UITiledSprite>();
			sprite.fillCenter = fillCenter;
		}
	}
	
	[XmlRoot("FilledSpriteNode")]
	public class FilledSpriteNode : SpriteNode
	{
		public UIFilledSprite.FillDirection fillDirection = UIFilledSprite.FillDirection.Radial360;
		public float fillAmount = 0;
		public bool invert = false;
		
		public FilledSpriteNode() : base()
		{
			fillDirection = UISprite.FillDirection.Radial360;
			fillAmount = 0;
			invert = false;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UIFilledSprite sprite = trans.GetComponent<UIFilledSprite>();
			fillDirection = sprite.fillDirection;
			fillAmount = sprite.fillAmount;
			invert = sprite.invert;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIFilledSprite sprite = trans.GetComponent<UIFilledSprite>();
			sprite.fillDirection = fillDirection;
			sprite.fillAmount = fillAmount;
			sprite.invert = invert;
		}
	}
	
	[XmlRoot("SimpleTextureNode")]
	public class SimpleTextureNode : Node
	{
		public SimpleTextureNode() : base()
		{
			
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
		}
	}
	
	[XmlRoot("ButtonNode")]
	public class ButtonNode : Node
	{
		public string bgControlName = "";
		public string lbControlName = "";
		
		public ButtonNode() : base()
		{
			bgControlName = "";
			lbControlName = "";
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
		}
	}
	
	[XmlRoot("ImageButtonNode")]
	public class ImageButtonNode : Node
	{
		public string normalSprite = "";
		public string hoverSprite = "";
		public string pressedSprite = "";
		
		public ImageButtonNode() : base()
		{
			normalSprite = "";
			hoverSprite = "";
			pressedSprite = "";
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UIImageButton button = trans.GetComponent<UIImageButton>();
			normalSprite = button.normalSprite;
			hoverSprite = button.hoverSprite;
			pressedSprite = button.pressedSprite;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIImageButton button = trans.GetComponent<UIImageButton>();
			button.normalSprite = normalSprite;
			button.hoverSprite = hoverSprite;
			button.pressedSprite = pressedSprite;
		}
	}
	
	[XmlRoot("CheckboxNode")]
	public class CheckboxNode : Node
	{
		public string functionName = "OnActivate";
		public bool startsChecked = true;
		public bool optionCanBeNone = false;
		
		public CheckboxNode() : base()
		{
			functionName = "OnActivate";
			startsChecked = true;
			optionCanBeNone = false;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UICheckbox box = trans.GetComponent<UICheckbox>();
			functionName = box.functionName;
			startsChecked = box.startsChecked;
			optionCanBeNone = box.optionCanBeNone;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UICheckbox box = trans.GetComponent<UICheckbox>();
			box.functionName = functionName;
			box.startsChecked = startsChecked;
			box.optionCanBeNone = optionCanBeNone;
		}
	}
	
	[XmlRoot("SliderNode")]
	public class SliderNode : Node
	{
		public float sliderValue = 0;
		public int numberOfSteps = 0;
		public Vector2 fullSize = Vector2.zero;
		public UISlider.Direction direction = UISlider.Direction.Horizontal;
		public string functionName = "OnSliderChange";
		
		public SliderNode() : base()
		{
			sliderValue = 0;
			numberOfSteps = 0;
			fullSize = Vector2.zero;
			direction = UISlider.Direction.Horizontal;
			functionName = "OnSliderChange";
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UISlider slider = trans.GetComponent<UISlider>();
			sliderValue = slider.sliderValue;
			numberOfSteps = slider.numberOfSteps;
			fullSize = slider.fullSize;
			direction = slider.direction;
			functionName = slider.functionName;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UISlider slider = trans.GetComponent<UISlider>();
			slider.sliderValue = sliderValue;
			slider.numberOfSteps = numberOfSteps;
			slider.fullSize = fullSize;
			slider.direction = direction;
			slider.functionName = functionName;
		}
	}
	
	[XmlRoot("InputNode")]
	public class InputNode : Node
	{
		public int maxChar = 0;
		public string caratChar = "|";
		public UIInput.KeyboardType type = UIInput.KeyboardType.Default;
		public bool isPassword = false;
		public Color activeColor = Color.white;
		public string functionName = "OnSubmit";
		
		public InputNode() : base()
		{
			maxChar = 0;
			caratChar = "|";
			type = UIInput.KeyboardType.Default;
			isPassword = false;
			activeColor = Color.white;
			functionName = "OnSubmit";
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UIInput input = trans.GetComponent<UIInput>();
			maxChar = input.maxChars;
			caratChar = input.caratChar;
			type = input.type;
			isPassword = input.isPassword;
			activeColor = input.activeColor;
			functionName = input.functionName;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIInput input = trans.GetComponent<UIInput>();
			input.maxChars = maxChar;
			input.caratChar = caratChar;
			input.type = type;
			input.isPassword = isPassword;
			input.activeColor = activeColor;
			input.functionName = functionName;
		}
	}
	
	[XmlRoot("PopListNode")]
	public class PopListNode : Node
	{
		public string backgroundSprite = "";
		public string highlightSprite = "";
		public string options = @"List Option 0
List Option 1
List Option 2
List Option 3
List Option 4";
		public string selection = "List Option 0";
		public UIPopupList.Position position = UIPopupList.Position.Auto;
		public float textScale = 1;
		public Color textColor = Color.white;
		public Color backgroundColor = Color.white;
		public Color highlightColor = new Color(152.0f/255, 1, 51.0f/255, 1);
		public bool isLocalized = false;
		public bool isAnimated = true;
		public Vector2 padding = Vector2.zero;
		public string functionName = "OnSelectionChange";
		
		public PopListNode() : base()
		{
			backgroundSprite = "";
			highlightSprite = "";
			options = @"List Option 0
List Option 1
List Option 2
List Option 3
List Option 4";
			selection = "List Option 0";
			position = UIPopupList.Position.Auto;
			textScale = 1;
			textColor = Color.white;
			backgroundColor = Color.white;
			highlightColor = new Color(152.0f/255, 1, 51.0f/255, 1);
			isLocalized = false;
			isAnimated = true;
			padding = Vector2.zero;
			functionName = "OnSelectionChange";
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UIPopupList list = trans.GetComponent<UIPopupList>();
			backgroundSprite = list.backgroundSprite;
			highlightSprite = list.highlightSprite;
			options = "";
			foreach(string s in list.items) options += s + "\n";
			selection = list.selection;
			position = list.position;
			textScale = list.textScale;
			textColor = list.textColor;
			backgroundColor = list.backgroundColor;
			highlightColor = list.highlightColor;
			isLocalized = list.isLocalized;
			isAnimated = list.isAnimated;
			padding = list.padding;
			functionName = list.functionName;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIPopupList list = trans.GetComponent<UIPopupList>();
			list.backgroundSprite = backgroundSprite;
			list.highlightSprite = highlightSprite;
			
			string[] split = options.Split(new char[]{'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
			list.items.Clear();
			foreach(string s in split) list.items.Add(s);
			
			list.selection = selection;
			list.position = position;
			list.textScale = textScale;
			list.textColor = textColor;
			list.backgroundColor = backgroundColor;
			list.highlightColor = highlightColor;
			list.isLocalized = isLocalized;
			list.isAnimated = isAnimated;
			list.padding = padding;
			list.functionName = functionName;
		}
	}
	
	[XmlRoot("ScrollBarNode")]
	public class ScrollBarNode : Node
	{
		public float scrollValue = 0;
		public float barSize = 0;
		public float alpha = 1;
		public UIScrollBar.Direction direction = UIScrollBar.Direction.Horizontal;
		public bool inverted = false;
		
		public ScrollBarNode() : base()
		{
			scrollValue = 0;
			barSize = 0;
			alpha = 1;
			direction = UIScrollBar.Direction.Horizontal;
			inverted = false;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
			UIScrollBar scrollBar = trans.GetComponent<UIScrollBar>();
			scrollValue = scrollBar.scrollValue;
			barSize = scrollBar.barSize;
			alpha = scrollBar.alpha;
			direction = scrollBar.direction;
			inverted = scrollBar.inverted;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
			UIScrollBar scrollBar = trans.GetComponent<UIScrollBar>();
			scrollBar.scrollValue = scrollValue;
			scrollBar.barSize = barSize;
			scrollBar.alpha = alpha;
			scrollBar.direction = direction;
			scrollBar.inverted = inverted;
		}
	}
	
	[XmlRoot("MixNode")]
	public class MixNode : Node
	{
		public int depth = 0;
		public Color color = Color.white;
		public UIWidget.Pivot pivot = UIWidget.Pivot.Center;
		
		public string text = "";
		public int lineWidth = 0;
		public bool multiLine = true;
//		public UILabel.TypingCustom typingCustom = UILabel.TypingCustom.Chinese;
		public int spacX = 0;
		public int spacY = 0;
		public bool password = false;
		public bool supportEncoding = true;
		public UILabel.Effect effectStyle = UILabel.Effect.None;
		public Color effectColor = Color.black;
		
		public MixNode() : base()
		{
			depth = 0;
			color = Color.white;
			pivot = UIWidget.Pivot.Center;
			
			text = "";
			lineWidth = 0;
			multiLine = true;
//			typingCustom = UILabel.TypingCustom.Chinese;
			spacX = 0;
			spacY = 0;
			password = false;
			supportEncoding = true;
			effectStyle = UILabel.Effect.None;
			effectColor = Color.black;
		}
		
		public override void SaveNodeInfo (Transform trans)
		{
			base.SaveNodeInfo (trans);
			
//			UIMix mix = trans.GetComponent<UIMix>();
//			
//			depth = mix.depth;
//			color = mix.color;
//			pivot = mix.pivot;
//			
//			text = mix.text;
//			lineWidth = mix.lineWidth;
//			multiLine = mix.multiLine;
//			typingCustom = mix.typingCustom;
//			spacX = mix.spacX;
//			spacY = mix.spacY;
//			password = mix.password;
//			supportEncoding = mix.supportEncoding;
//			effectStyle = mix.effectStyle;
//			effectColor = mix.effectColor;
		}
		
		public override void UpdateNodeInfo (Transform trans)
		{
			base.UpdateNodeInfo (trans);
			
//			UIMix mix = trans.GetComponent<UIMix>();
//			
//			mix.depth = depth;
//			mix.color = color;
//			mix.pivot = pivot;
//			
//			mix.text = text;
//			mix.lineWidth = lineWidth;
//			mix.multiLine = multiLine;
//			mix.typingCustom = typingCustom;
//			mix.spacX = spacX;
//			mix.spacY = spacY;
//			mix.password = password;
//			mix.supportEncoding = supportEncoding;
//			mix.effectStyle = effectStyle;
//			mix.effectColor = effectColor;
		}
	}
	
	public enum NodeType
	{
		
		Normal,
		Window,
		UIPanel,
		
		UIMix,
		UIButton,
		UIImageButton,
		UICheckbox,
		UISlider,
		UIInput,
		UIPopList,
		UIScrollBar,
		
		UILabel,
		
		UIFilledSprite,
		UITiledSprite,
		UISlicedSprite,
		UISprite,
		
		UITexture,
	}
}
